//
//  BoardNameFormatter.m
//  PicUploader
//
//  Created by Chen Zhang on 9/13/08.
//  Copyright 2008 overboming. All rights reserved.
//

#import "BoardNameFormatter.h"

@implementation BoardNameFormatter


- (id)init
{
    self = [super init];
    if(self){
        NSDictionary *helper = [[NSDictionary dictionaryWithObjectsAndKeys:
                                 @"FDU_Development",@"FDU_Development",
                                 @"Undergraduate",@"Undergraduate",
                                 @"FDU_C.S.E.",@"FDU_C.S.E.",
                                 @"FDU_E.E.",@"FDU_E.E.",
                                 @"FDU_Economics",@"FDU_Economics",
                                 @"FDU_F.L.",@"FDU_F.L.",
                                 @"FDU_Law",@"FDU_Law",
                                 @"FDU_Management",@"FDU_Management",
                                 @"FDU_Philosophy",@"FDU_Philosophy",
                                 @"Mathematics_04",@"Mathematics_04",
                                 @"Physics_03",@"Physics_03",
                                 @"Mac",@"Mac",
                                 @"MS_Windows",@"MS_Windows",
                                 @"Software",@"Software",
                                 @"NR_Movie",@"NR_Movie",
                                 @"Unix",@"Unix",
                                 @"Virus",@"Virus",
                                 @"Animal",@"Animal",
                                 @"Auto",@"Auto",
                                 @"Beauty",@"Beauty",
                                 @"MobilePhone",@"MobilePhone",
                                 @"City_Cafe",@"City_Cafe",
                                 @"Flower",@"Flower",
                                 @"Food",@"Food",
                                 @"Music_Game",@"Music_Game",
                                 @"PC_Game",@"PC_Game",
                                 @"Photography",@"Photography",
                                 @"PIC",@"PIC",
                                 @"Sports_Game",@"Sports_Game",
                                 @"Travel",@"Travel",
                                 @"Video_Game",@"Video_Game",
                                 @"Calligraphy",@"Calligraphy",
                                 @"Campus_Digest",@"Campus_Digest",
                                 @"Fantasy",@"Fantasy",
                                 @"Fine_Arts",@"Fine_Arts",
                                 @"Basketball",@"Basketball",
                                 @"Card",@"Card",
                                 @"Cycling",@"Cycling",
                                 @"Football_CN",@"Football_CN",
                                 @"Football_WD",@"Football_WD",
                                 @"Keepfit",@"Keepfit",
                                 @"Outdoors",@"Outdoors",
                                 @"Running",@"Running",
                                 @"Speed",@"Speed",
                                 @"Employees",@"Employees",
                                 @"Marriage",@"Marriage",
                                 @"Single",@"Single",
                                 @"DigitalLife",@"DigitalLife",
                                 @"Magpie_Bridge",@"Magpie_Bridge",
                                 @"Traffic_Info",@"Traffic_Info",
                                 @"Astronomy",@"Astronomy",
                                 @"Architecture",@"Architecture",
                                 @"Art_Study",@"Art_Study",
                                 @"EE",@"EE",
                                 @"Foreign_Language",@"Foreign_Language",
                                 @"French",@"French",
                                 @"Geography",@"Geography",
                                 @"History",@"History",
                                 @"Insurance_Act",@"Insurance_Act",
                                 @"MathTools",@"MathTools",
                                 @"ME",@"ME",
                                 @"Military",@"Military",
                                 @"Nature",@"Nature",
                                 @"Num_Analysis",@"Num_Analysis",
                                 @"TeX",@"TeX",
                                 @"Chinese_Music",@"Chinese_Music",
                                 @"Classic",@"Classic",
                                 @"NR_Cartoon",@"NR_Cartoon",
                                 @"NR_Music",@"NR_Music",
                                 @"OMTV",@"OMTV",
                                 @"TV",@"TV",
                                 @"FM_Beauty_Dress",@"FM_Beauty_Dress",
                                 @"FM_Commodity",@"FM_Commodity",
                                 @"FM_Ecommodity",@"FM_Ecommodity",
                                 @"FM_PC",@"FM_PC",
                                 @"MerchantAgent",@"MerchantAgent",nil] retain]; 
        
        

        
        boardNameDic = [[NSDictionary alloc] initWithDictionary:helper];    
        
    }
    
    return self;
}

- (NSString *)stringForObjectValue:(id)obj
{
    if (![obj isKindOfClass:[NSString class]]) {
        return nil;
    }
    else{
        return [NSString stringWithFormat:@"%@",obj];
    }
}

- (void)dealloc
{
    [boardNameDic release];
    [super dealloc];
}


- (BOOL)getObjectValue:(id *)obj
             forString:(NSString *)string
      errorDescription:(NSString **)errorString
{
    // Look up the color for 'string'
    NSString *matchingKey = [self firstBoareNameForPartialString:string];
    if (matchingKey) {
//        *obj = [boardNameDic objectForKey:matchingKey];
		*obj = matchingKey;
        return YES;
    } else {
        // Occasionally, 'errorString' is NULL
        if (errorString != NULL) {
            *errorString = [NSString stringWithFormat:
                            @"'%@' is not a valid board name", string];
        }
        return NO;
    }
}

- (BOOL)isPartialStringValid:(NSString *)partial
            newEditingString:(NSString **)newString
            errorDescription:(NSString **)error
{
    if ([partial length] == 0){
        return YES;
    }
    NSString *match = [self firstBoareNameForPartialString:partial];
    if (match) {
        return YES;
    } else {
        if (error) {
            *error = @"No such board name";
        }
        return NO;
    }
    
}


- (BOOL)isPartialStringValid:(NSString **)partial
       proposedSelectedRange:(NSRange *)selPtr
              originalString:(NSString *)origString
       originalSelectedRange:(NSRange)origSel
            errorDescription:(NSString **)error
{
    // Zero-length strings are fine
    if ([*partial length] == 0) {
        return YES;
    }
    NSString *match = [self firstBoareNameForPartialString:*partial];
    // No color match?
    if (!match) {
        return NO;
    }
    
    // If this would not move the beginning of the selection, it
    // is a delete
    if (origSel.location == selPtr->location) {
        return YES;
    }
    
    // If the partial string is shorter than the
    // match, provide the match and set the selection
    if ([match length] != [*partial length]) {
        selPtr->location = [*partial length];
        selPtr->length = [match length] - selPtr->location;
        *partial = match;
        return NO;
    }
    return YES;
}

- (NSString *)firstBoareNameForPartialString:(NSString *)string
{
    // Is the key zero-length?
    if ([string length] == 0) {
        return nil;
    }
    for (NSString *key in [boardNameDic allKeys]) {
        NSRange whereFound = [key rangeOfString:string
                                        options:NSCaseInsensitiveSearch];
        // Does the string match the beginning of the board name?
        if ((whereFound.location == 0) && (whereFound.length > 0)) {
            return key;
        }
    }
    // If no match is found, return nil
    return nil;
}

@synthesize boardNameDic;
@end
